package com.lt.main;

import java.util.ArrayList;
import java.util.Stack;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

public class LightsToggleActivity extends Activity {
	
	private ArrayList<ArrayList<CheckBox>> _checkPuzzle;
	private Puzzle _puzzleToSolve;
	private ProgressBar _pb;
	
	private enum Mode{
		EDIT,
		PLAY;
	}
	
	private Mode _curMode;
	
	private Button _editButton, _solveButton;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
         
        _checkPuzzle = new ArrayList<ArrayList<CheckBox>>();
        _checkPuzzle.add(new ArrayList<CheckBox>());
        _checkPuzzle.get(0).add((CheckBox)findViewById(R.id.CheckBox1));
        _checkPuzzle.get(0).add((CheckBox)findViewById(R.id.CheckBox2));
        _checkPuzzle.get(0).add((CheckBox)findViewById(R.id.CheckBox3));
        
        _checkPuzzle.add(new ArrayList<CheckBox>());
        _checkPuzzle.get(1).add((CheckBox)findViewById(R.id.CheckBox4));
        _checkPuzzle.get(1).add((CheckBox)findViewById(R.id.CheckBox5));
        _checkPuzzle.get(1).add((CheckBox)findViewById(R.id.CheckBox6));
        
        _checkPuzzle.add(new ArrayList<CheckBox>());
        _checkPuzzle.get(2).add((CheckBox)findViewById(R.id.CheckBox7));
        _checkPuzzle.get(2).add((CheckBox)findViewById(R.id.CheckBox8));
        _checkPuzzle.get(2).add((CheckBox)findViewById(R.id.CheckBox9));
        
    	for(int y = 0; y < 3; y++)
    	{
    		for(int x = 0; x < 3; x++)
    		{
    			_checkPuzzle.get(y).get(x).setOnClickListener(_checkBoxHandler);
    		}
    	}
        
        _editButton = (Button)findViewById(R.id.EditButton);
        _editButton.setOnClickListener(_editButtonHandler);
        _solveButton = (Button)findViewById(R.id.SolveButton);
        _solveButton.setOnClickListener(_solveButtonHandler);
        
        _curMode = Mode.PLAY;
        _puzzleToSolve = new Puzzle(3,3);
        _pb = new ProgressBar(this);
    }
    
    Button.OnClickListener _editButtonHandler = new Button.OnClickListener()
    {
    	public void onClick(View v)
    	{
    		_editButton.setEnabled(false);
    		_curMode = Mode.EDIT;
    	}
    };
    
    Button.OnClickListener _solveButtonHandler = new Button.OnClickListener()
    {
    	public void onClick(View v)
    	{
    		_editButton.setEnabled(true);
    		_curMode = Mode.PLAY;
    		LinearLayout hv = ((LinearLayout)findViewById(R.id.solutionLinearLayout));
    		hv.removeAllViews();
    		
    		SyncPuzzle();
    		if(_puzzleToSolve.IsPuzzleDone())
    		{
    			
    		}
    		Solver test = new Solver(_puzzleToSolve);
    		_pb.setVisibility(View.VISIBLE);
    		test.start();
    		while(test._ableToSolve == null)
    		{
    			
    		}
    		_pb.setVisibility(View.GONE);
    		if(test._ableToSolve)
    		{
	    		Stack<Puzzle> finalSolution = test.GetSolutionTree();
	    		while(!finalSolution.isEmpty())
	    		{
	    			AddPuzzleToUI(finalSolution.peek());
	    			finalSolution.pop();
	    		}
    		}
    		else
    		{
    			Toast toast=Toast.makeText(LightsToggleActivity.this,"Hello toast", 2000);
    		     toast.setGravity(Gravity.TOP, -30, 50);
    		     toast.show();
    		}
    		
    	}
    };
    
    private void SetAlreadySolvedMessage()
    {
    	LinearLayout hv = ((LinearLayout)findViewById(R.id.solutionLinearLayout));
		TextView tv = new TextView(this);
		tv.setText("Its already solved.");
		hv.addView(tv);
		hv.invalidate();
    }
    
    private void AddPuzzleToUI(Puzzle p)
    {
    	LinearLayout puz = new LinearLayout(this);
    	puz.setClickable(false);
    	puz.setOrientation(LinearLayout.VERTICAL);
    	
    	for(int y = 0; y < 3; y++)
    	{
    		LinearLayout line = new LinearLayout(this);
    		for(int x = 0; x < 3; x++)
    		{
    			if((y == p._comY) && (x == p._comX))
    			{
    				RadioButton r = new RadioButton(this);
    				r.setChecked(p._grid[y][x]);
        			r.setClickable(false);
        			line.addView(r);
    			}
    			else
    			{
    				CheckBox c = new CheckBox(this);
    				c.setChecked(p._grid[y][x]);
        			c.setClickable(false);
        			line.addView(c);
    			}
    		}
    		puz.addView(line);
    	}
    	LinearLayout hv = ((LinearLayout)findViewById(R.id.solutionLinearLayout));
    	puz.setPadding(10, 5, 10, 0);
    	hv.addView(puz);
    	
    	View iv = new View(this);
    	iv.setBackgroundColor(0xFF0000FF);
    	hv.addView(iv, new LinearLayout.LayoutParams(1, ViewGroup.LayoutParams.FILL_PARENT));
    	
    	hv.invalidate();
    }
    
    private void SyncPuzzle()
    {
    	for(int y = 0; y < 3; y++)
    	{
    		for(int x = 0; x < 3; x++)
    		{
    			_puzzleToSolve._grid[y][x] = _checkPuzzle.get(y).get(x).isChecked();
    		}
    	}
    }
    
    CheckBox.OnClickListener _checkBoxHandler = new CheckBox.OnClickListener()
    {
    	public void onClick(View v)
    	{
    		LinearLayout hv = ((LinearLayout)findViewById(R.id.solutionLinearLayout));
    		hv.removeAllViews();
    		
    		//if edit, just toggle
    		switch( _curMode )
    		{
	    		case EDIT:
	    			break;
	    			
	    		case PLAY:
	    			int[] cds = GetPosInArray(v);
	    			if(cds[1] < 2)
	    				_checkPuzzle.get(cds[1] +1).get(cds[0]).toggle();
	    			if(cds[0] < 2)
	    				_checkPuzzle.get(cds[1]).get(cds[0]+1).toggle();
	    			if(cds[1] > 0)
	    				_checkPuzzle.get(cds[1] -1).get(cds[0]).toggle();
	    			if(cds[0] > 0)
	    				_checkPuzzle.get(cds[1]).get(cds[0]-1).toggle();
	    			break;
    		}
    	}
    };
    
    private int[] GetPosInArray( View v )
    {
    	
    	for(int y = 0; y < 3; y++)
    	{
    		for(int x = 0; x < 3; x++)
    		{
    			if(_checkPuzzle.get(y).get(x).getId() == v.getId())
    			{
    				int[] coords = {x,y}; 
    				return coords;
    			}
    		}
    	}
    	return null;
    }
}